package com.zqxq.lottery.api.controller;

import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.DispatcherUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.common.utils.ValidateUtils;
import com.zqxq.common.utils.WebUtils;
import com.zqxq.lottery.api.feign.LotteryServerFeignClient;
import com.zqxq.lottery.api.utils.MobileUtil;
import com.zqxq.lottery.api.utils.UserCenterHttps;
import com.zqxq.lottery.entity.bo.SmsBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description ：
 * @Author ： 解风
 * @Date ： 2018/01/04 20:57
 */
@Api(value = "短信接口",tags = "短信接口")
@RestController
@Slf4j
@RequestMapping(value="/sms",produces = "application/json")
public class SmsController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private LotteryServerFeignClient lotteryServerFeignClient;

    @Autowired
    private UserCenterHttps userCenterHttps;

    /**
     * 添加设备信息
     * @param smsBO
     * @return
     */
    @PostMapping("/sendVerifyCode")
    @ApiOperation(value = "添加设备信息", notes = "添加设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码",paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "codeType", value = "验证码类型：0注册、1登录、2修改密码",  paramType = "query", dataType = "Integer", required = true)
    })
    public Result sendVerifyCode(HttpServletRequest request, SmsBO smsBO) {
        log.info("手机用户【" + smsBO.getMobile() + "】正在发送短信" + ",用户所在" + smsBO.getAppKey() + "平台");
        Result result = new Result();
        try {
            String regIp = WebUtils.getClientIP(request);
            String mobile = smsBO.getMobile();
            Integer codeType = smsBO.getCodeType();
            if (StrUtils.isEmpty(mobile) || !MobileUtil.isMobile(mobile) || codeType == null) {
                return new Result(CodeEnum.FAIL_100002.getCode(), "手机号码错误，请重新输入");
            }
            result = userCenterHttps.sendSms(smsBO);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据旧密码修改密码失败！", e);
            return new Result(CodeEnum.FAIL_100005.getCode(), CodeEnum.FAIL_100005.getMsg());
        }
    }

    /**
     * 发送短信
     * @param smsBO
     * @return
     */
    @PostMapping("/sendMsg")
    @ApiOperation(value = "发送短信", notes = "发送短信")
    public void sendMsg(@RequestBody SmsBO smsBO) {
        userCenterHttps.sendMsg(smsBO);
    }

    /**
     * h5发送验证码
     * @param smsBO
     * @return
     */
    @PostMapping("/h5SendVerifyCode")
    @ApiOperation(value = "添加设备信息", notes = "添加设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码",paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "codeType", value = "验证码类型：0注册、1登录、2修改密码",  paramType = "query", dataType = "Integer", required = true)
    })
    public Result h5SendVerifyCode(HttpServletRequest request, SmsBO smsBO) {
        log.info("手机用户【" + smsBO.getMobile() + "】正在发送短信" + ",用户所在" + smsBO.getAppKey() + "平台");
        Result result = new Result();
        try {
            String regIp = WebUtils.getClientIP(request);
            String mobile = smsBO.getMobile();
            Integer codeType = smsBO.getCodeType();
            if (StrUtils.isEmpty(mobile) || !MobileUtil.isMobile(mobile) || codeType == null) {
                return new Result(CodeEnum.FAIL_100002.getCode(), "手机号码错误，请重新输入");
            }
            //验证手机号是否注册
            Result rs = lotteryServerFeignClient.getUserInfoByMobile(mobile);
            Integer count = DispatcherUtils.parseInteger(rs.getData());
            if(count>0){
                return new Result(100006, "该手机号已经注册！");
            }
            //图形验证码校验
            String imgCode = smsBO.getImgCode();
            if (StrUtils.isEmpty(imgCode)){
                return new Result(CodeEnum.FAIL_100002.getCode(), "图形验证码不能为空");
            }
            String c = redisTemplate.opsForValue().get("code" + imgCode.toUpperCase());
//            if (StrUtils.isEmpty(c))
//                return new Result(CodeEnum.FAIL.getCode(), "图形验证码已过期");
            if (!imgCode.equalsIgnoreCase(c))
                return new Result(CodeEnum.FAIL.getCode(), "图形验证码错误");
            result = userCenterHttps.sendSms(smsBO);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据旧密码修改密码失败！", e);
            return new Result(CodeEnum.FAIL_100005.getCode(), CodeEnum.FAIL_100005.getMsg());
        }
    }

}
